/*
 * @Author: wupeiwen <javapeiwen2010@gmail.com>
 * @Date: 2022-05-25 16:02:44
 * @LastEditors: wupeiwen <javapeiwen2010@gmail.com>
 * @LastEditTime: 2023-06-29 14:31:42
 * @FilePath: /tea-garden-web/src/util/storeModules/subject-weather.js
 * @Description: 专题-气象灾害
 */
import { get } from '@/util/http.js'
export default {
  state: {
    types: [{ name: '霜冻害', value: '1' }, { name: '热害', value: '2' }],
    activeType: '1',
    loading: {
      weatherWarn: false,
      temperatureTrend: false,
      realTimeWeather: false,
      farmlandWeatherWarn: false
    },
    weatherWarn: null,
    temperatureTrend: [],
    realTimeWeather: null,
    farmlandWeatherWarn: []
  },
  mutations: {
    setActiveType (state, payload) {
      state.activeType = payload
    },
    // 设置通用状态
    setCommonState (state, payload) {
      const { key, value } = payload
      state[key] = value
    },
    // 设置加载状态
    setLoadingState (state, payload) {
      const { key, value } = payload
      state.loading[key] = value
    }
  },
  actions: {
    // 气象灾害预警
    async getWeatherWarn ({ state, commit, rootState }) {
      try {
        commit('setLoadingState', { key: 'weatherWarn', value: true })
        commit('setCommonState', { key: 'weatherWarn', value: null })
        const { data } = await get({ url: '/Meteorology/MeteorologyWarn', params: { tab_type: state.activeType, area_id: rootState['base-overview'].currentFarmlandId } })
        commit('setCommonState', { key: 'weatherWarn', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'weatherWarn', value: false })
    },
    // 温度趋势变化
    async getTemperatureTrend ({ commit }) {
      try {
        commit('setLoadingState', { key: 'temperatureTrend', value: true })
        commit('setCommonState', { key: 'temperatureTrend', value: [] })
        const { data } = await get({ url: '/Meteorology/TemperatureTrend' })
        const temperatureTrend = data.reverse().map((item) => {
          const date = item.date.split('-')
          item.name = date[1] + '.' + date[2] + ' ' + item.hour.split(':')[0] + '时'
          return { name: item.name, value: item.temperature }
        })
        console.log(temperatureTrend)
        commit('setCommonState', { key: 'temperatureTrend', value: temperatureTrend })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'temperatureTrend', value: false })
    },
    // 实时温度
    async getRealTimeWeather ({ commit }) {
      try {
        commit('setLoadingState', { key: 'realTimeWeather', value: true })
        commit('setCommonState', { key: 'realTimeWeather', value: null })
        const { data } = await get({ url: '/Meteorology/RealTemperature' })
        commit('setCommonState', { key: 'realTimeWeather', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'realTimeWeather', value: false })
    },
    // 地块天气信息
    async getFarmlandWeatherWarn ({ state, commit }) {
      try {
        commit('setLoadingState', { key: 'farmlandWeatherWarn', value: true })
        commit('setCommonState', { key: 'farmlandWeatherWarn', value: [] })
        const { data } = await get({ url: '/Meteorology/TemperatureMassifDistribute', params: { tab_type: state.activeType } })
        commit('setCommonState', { key: 'farmlandWeatherWarn', value: data })
      } catch (error) {
        console.log(error)
      }
      commit('setLoadingState', { key: 'farmlandWeatherWarn', value: false })
    }
  }
}
